package com.ssyc.common.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.lzy.ninegrid.ImageInfo;
import com.ssyc.common.R;
import com.ssyc.common.activity.ShowBigImagePreviewActivity;

import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2018/1/18 0018.
 */

public class ShowImagePreviewAdapter extends PagerAdapter implements
        PhotoViewAttacher.OnPhotoTapListener {

    private List<ImageInfo> imageInfo;
    private Context context;
    private View currentView;

    public ShowImagePreviewAdapter(Context context,
                                   @NonNull List<ImageInfo> imageInfo) {
        super();
        this.imageInfo = imageInfo;
        this.context = context;
    }

    @Override
    public int getCount() {
        return imageInfo.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        currentView = (View) object;
    }

    public View getPrimaryItem() {
        return currentView;
    }

    public ImageView getPrimaryImageView() {
        return (ImageView) currentView.findViewById(R.id.pv);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.item_photoview, container, false);
        final ProgressBar pb = (ProgressBar) view.findViewById(R.id.pb);
        final PhotoView imageView = (PhotoView) view.findViewById(R.id.pv);

        ImageInfo info = this.imageInfo.get(position);
        imageView.setOnPhotoTapListener((PhotoViewAttacher.OnPhotoTapListener) this);
        showExcessPic(info, imageView);

        Glide.with(context).load(info.bigImageUrl).into(imageView);

        pb.setVisibility(View.GONE);
//         Glide.with(context).load(info.bigImageUrl)//
////         .placeholder(R.drawable.ic_default_image)//
////         .error(R.drawable.ic_default_image)//
//         .diskCacheStrategy(DiskCacheStrategy.ALL)//
//         .listener(new RequestListener<String, GlideDrawable>() {
//         @Override
//         public boolean onException(Exception e, String model,
//         Target<GlideDrawable> target, boolean isFirstResource) {
//         pb.setVisibility(View.GONE);
//         return false;
//         }
//
//         @Override
//         public boolean onResourceReady(GlideDrawable resource, String model,
//         Target<GlideDrawable> target, boolean isFromMemoryCache, boolean
//         isFirstResource) {
//         pb.setVisibility(View.GONE);
//         return false;
//         }
//         }).into(imageView);

        container.addView(view);
        return view;
    }

    /**
     * 展示过度图片
     */
    private void showExcessPic(ImageInfo imageInfo, PhotoView imageView) {
        // 先获取大图的缓存图片
        // Bitmap cacheImage =
        // NineGridView.getImageLoader().getCacheImage(imageInfo.bigImageUrl);
        // //如果大图的缓存不存在,在获取小图的缓存
        // if (cacheImage == null) cacheImage =
        // NineGridView.getImageLoader().getCacheImage(imageInfo.thumbnailUrl);
        // //如果没有任何缓存,使用默认图片,否者使用缓存
        // if (cacheImage == null) {
        // imageView.setImageResource(R.drawable.defaultimage);
        // } else {
        // imageView.setImageBitmap(cacheImage);
        // }
        imageView.setImageResource(R.drawable.defaultimage);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }


    /**
     * 单击屏幕关闭
     */
    @Override
    public void onPhotoTap(View view, float x, float y) {
        ((ShowBigImagePreviewActivity) context).finish();
    }

}
